import React,{useRef,forwardRef,useImperativeHandle} from 'react'

 function Child4({setMsg},ref) {

    const msgInput = useRef();


  useImperativeHandle(ref, () => ({
    getMsg: () => { //暴露子组件的方法给 父组件
      return  msgInput.current.value;
    },
    setMsg:(msg)=>{
        msgInput.current.value = msg;
    }
  }));

  return (
    <div>Child4
        <div>内容:<input ref={msgInput}/>
            <button onClick={()=>{
                setMsg(msgInput.current.value);
            }}>给父组件</button>
        </div>
    </div>
  )
}
export default forwardRef(Child4);
